/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.servlet;



import com.dao.DAOTransacciones;
import com.mail.Mail;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author Botica Colonial
 */
public class SLogin extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String accionStr = request.getParameter("accion") == null ? "20" : request.getParameter("accion");
            int accion = Integer.parseInt(accionStr);
            BOEmpleado bEmpleado = null;
            BOSesion bSesion = null;
            ArrayList<String> datos = new ArrayList<String>();
            DAOLogin dLogin = new DAOLogin();
            DAOSesion dSesion = new DAOSesion();
            DAOTransacciones dtransacciones = new DAOTransacciones();
            int cont = 0;

            String pagina = "";

            switch (accion) {
                case 0: // Caso en el cual se regresará a la ventana de inicio de la aplicación.

                    pagina = "/index.jsp";
                    break;

                case 1:// Se redirecciona a el menu principal.


                    String user = String.valueOf(request.getParameter("user"));
                    String pass = String.valueOf(request.getParameter("pass"));


                    bEmpleado = dLogin.ValidarUsuario(user, pass);

                    if (bEmpleado != null) {



                        if (bEmpleado.getCargo().equalsIgnoreCase("Administrador")) {//Administrador

                            dSesion.RegistraSesionAdmin(bEmpleado);
                            bSesion = dSesion.ObtenerSesionAdmin();
                            HttpSession session = request.getSession(true);
                            session.setAttribute("sesion", bSesion);
                            session.setAttribute("usuario", bEmpleado);

                            pagina = "/Admin/menuAdmin.jsp";



                        } else if (bEmpleado.getCargo().equalsIgnoreCase("Empleado")) {//Empleado



                            if (dSesion.ExtraerSalida() == null) {

                                
                                if(dSesion.ExtraerEmpleadoSalida()!=bEmpleado.getDNI()){
                                    pagina = "/PopUps/noCerroSesion.jsp";
                                    break;
                                }
                                else{dSesion.CerrarSesionMal();}
                                
                                

                            }

                            datos = dtransacciones.ExtraerSencilloActualizado();
                            double santerior = Double.parseDouble(datos.get(0));
                            double tasacambio = Double.parseDouble(datos.get(1));
                            double nose = dtransacciones.ExtraerSencilloDejado();
                            dSesion.RegistraSesion(bEmpleado, tasacambio);
                            bSesion = dSesion.ObtenerSesion();
                            HttpSession session = request.getSession(true);

                            request.setAttribute("tasacambio", tasacambio);
                            request.setAttribute("sencilloAnterior", santerior);
                            session.setAttribute("usuario", bEmpleado);
                            session.setAttribute("sesion", bSesion);
                            pagina = "/Ventas/inicioTurno.jsp";

                        }


                    } else {
                        pagina = "/PopUps/wrongLogin.jsp";
                    }

                    break;

                case 2:


                    dSesion.CerrarSesionMal();

                    pagina = "/index.jsp";
                    break;



                case 3:// Se redirecciona a el menu principal.

                    datos = dtransacciones.ExtraerSencilloActualizado();
                    double santerior = Double.parseDouble(datos.get(0));
                    double tasacambio = Double.parseDouble(datos.get(1));

                    request.setAttribute("tasacambio", tasacambio);
                    request.setAttribute("sencilloAnterior", santerior);

                    pagina = "/Ventas/inicioTurno.jsp";






                    break;


            }

            RequestDispatcher rd = getServletContext().getRequestDispatcher(pagina);
            rd.forward(request, response);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
